#!/bin/sh

# Proprietary configure script of RFC5766-TURN-Server project

cleanup() {
	rm -rf ${TMPCPROGC}
	rm -rf ${TMPCPROGB}
	rm -rf ${TH_TMPCPROGC}
	rm -rf ${TH_TMPCPROGB}
	rm -rf ${DTLS_TMPCPROGC}
	rm -rf ${DTLS_TMPCPROGB}
	rm -rf ${PQ_TMPCPROGC}
	rm -rf ${PQ_TMPCPROGB}
	rm -rf ${MYSQL_TMPCPROGC}
	rm -rf ${MYSQL_TMPCPROGB}
	rm -rf ${D_TMPCPROGC}
	rm -rf ${D_TMPCPROGB}
	rm -rf ${E_TMPCPROGC}
	rm -rf ${E_TMPCPROGO}
	rm -rf ${HR_TMPCPROGC}
	rm -rf ${HR_TMPCPROGB}
	rm -rf ${TMPCADDRPROGO}
}

testlibraw() {
    ${CC} ${TMPCPROGC} -o ${TMPCPROGB} ${OSCFLAGS} ${OSLIBS} -${1} 2>>/dev/null
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "Do not use -${1}"
		return 0
    else
		OSLIBS="${OSLIBS} -${1}"
		return 1
    fi
}

testlibevent2_comp() {
    ${CC} -c ${E_TMPCPROGC} -o ${E_TMPCPROGO} ${OSCFLAGS} 2>>/dev/null
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "Libevent2 development is not installed properly"
		return 0
    else
		return 1
    fi
}

testhiredis() {
    HIREDISCFLAGS=
    HIREDISLIBS=-lhiredis
    ${CC} ${HR_TMPCPROGC} -o ${HR_TMPCPROGB} ${OSCFLAGS} ${DBLIBS} ${HIREDISCFLAGS} ${HIREDISLIBS} 2>>/dev/null
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
	${ECHO_CMD}
	${ECHO_CMD} "HIREDIS DEVELOPMENT LIBRARY (libhiredis.*) AND/OR HEADERS (hiredis/*.h)"
	${ECHO_CMD} "	ARE NOT INSTALLED PROPERLY ON THIS SYSTEM."
	${ECHO_CMD} "	THAT'S OK BUT THE TURN SERVER IS BUILDING WITHOUT REDIS SUPPORT."
	${ECHO_CMD}
	return 0
    else
	DBCFLAGS="${DBCFLAGS} ${HIREDISCFLAGS}"
	DBLIBS="${DBLIBS} ${HIREDISLIBS}"
	return 1
    fi
}

testlibpq() {
    POSTCFLAGS="-I${PREFIX}/pgsql/include -I${PREFIX}/include/pgsql/ -I${PREFIX}/include/postgres/ -I${PREFIX}/postgres/include/ -I${PREFIX}/include/postgresql/ -I${PREFIX}/postgresql/include/"
    POSTCFLAGS="${POSTCFLAGS} -I/usr/local/pgsql/include -I/usr/local/include/pgsql/ -I/usr/local/include/postgres/ -I/usr/local/postgres/include/ -I/usr/local/include/postgresql/ -I/usr/local/postgresql/include/"
    POSTCFLAGS="${POSTCFLAGS} -I/usr/pgsql/include -I/usr/include/pgsql/ -I/usr/include/postgres/ -I/usr/postgres/include/ -I/usr/include/postgresql/ -I/usr/postgresql/include/"
    for ilib in ${PREFIX}/pgsql/lib ${PREFIX}/lib/pgsql ${PREFIX}/lib64/pgsql /usr/local/pgsql/lib /usr/local/lib/pgsql /usr/local/lib64/pgsql /usr/pgsql/lib /usr/lib/pgsql /usr/lib64/pgsql ${PREFIX}/postgres/lib ${PREFIX}/lib/postgres ${PREFIX}/lib64/postgres /usr/local/postgres/lib /usr/local/lib/postgres /usr/local/lib64/postgres /usr/postgres/lib /usr/lib/postgres /usr/lib64/postgres ${PREFIX}/postgresql/lib ${PREFIX}/lib/postgresql ${PREFIX}/lib64/postgresql /usr/local/postgresql/lib /usr/local/lib/postgresql /usr/local/lib64/postgresql /usr/postgresql/lib /usr/lib/postgresql /usr/lib64/postgresql
    do
	if [ -d ${ilib} ] ; then
    	    POSTLIBS="${POSTLIBS} -L${ilib}"
	    if ! [ -z "${TURN_ACCEPT_RPATH}" ] ; then
		TURN_RPATH="${TURN_RPATH} -Wl,-rpath,${ilib}"
	    fi
	fi
    done
    POSTLIBS="${OSLIBS} ${POSTLIBS} -lpq"
    ${CC} ${PQ_TMPCPROGC} -o ${PQ_TMPCPROGB} ${OSCFLAGS} ${DBCFLAGS} ${POSTCFLAGS} ${DBLIBS} ${POSTLIBS} ${OSLIBS} 2>>/dev/null 
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
    	${ECHO_CMD}
		${ECHO_CMD} "POSTGRESQL DEVELOPMENT LIBRARY (libpq.a) AND/OR HEADER (libpq-fe.h)"
		${ECHO_CMD} "	ARE NOT INSTALLED PROPERLY ON THIS SYSTEM."
		${ECHO_CMD} "	THAT'S OK BUT THE TURN SERVER IS BUILDING WITHOUT POSTGRESQL DATABASE SUPPORT."
		${ECHO_CMD}
		return 0
    else
		DBCFLAGS="${DBCFLAGS} ${POSTCFLAGS}"
		DBLIBS="${DBLIBS} ${POSTLIBS}"
		return 1
    fi
}

testlibmysql() {
    MYSQL_CFLAGS="-I${PREFIX}/mysql/include -I${PREFIX}/include/mysql/"
    MYSQL_CFLAGS="${MYSQL_CFLAGS} -I/usr/local/mysql/include -I/usr/local/include/mysql/"
    MYSQL_CFLAGS="${MYSQL_CFLAGS} -I/usr/mysql/include -I/usr/include/mysql/"
    for ilib in ${PREFIX}/mysql/lib ${PREFIX}/lib/mysql ${PREFIX}/lib64/mysql /usr/local/mysql/lib /usr/local/lib/mysql /usr/local/lib64/mysql /usr/mysql/lib /usr/lib/mysql /usr/lib64/mysql
    do
      if [ -d ${ilib} ] ; then
        MYSQL_LIBS="${MYSQL_LIBS} -L${ilib}"
	if ! [ -z "${TURN_ACCEPT_RPATH}" ] ; then
	    TURN_RPATH="${TURN_RPATH} -Wl,-rpath,${ilib}"
	fi
      fi
    done
    MYSQL_LIBS="${OSLIBS} ${MYSQL_LIBS} -lmysqlclient"
    ${CC} ${MYSQL_TMPCPROGC} -o ${MYSQL_TMPCPROGB} ${OSCFLAGS} ${DBCFLAGS} ${DBLIBS} ${MYSQL_CFLAGS} ${MYSQL_LIBS} ${OSLIBS} 2>>/dev/null
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
    	${ECHO_CMD}
		${ECHO_CMD} "MYSQL DEVELOPMENT LIBRARY (libmysqlclient) AND/OR HEADER (mysql.h)"
		${ECHO_CMD} "	ARE NOT INSTALLED PROPERLY ON THIS SYSTEM."
		${ECHO_CMD} "	THAT'S OK BUT THE TURN SERVER IS BUILDING WITHOUT MYSQL DATABASE SUPPORT."
		${ECHO_CMD}
		return 0
    else
		DBCFLAGS="${DBCFLAGS} ${MYSQL_CFLAGS}"
		DBLIBS="${DBLIBS} ${MYSQL_LIBS}"
		return 1
    fi
}

testlib() {
    testlibraw l${1}
}

pthread_testlib() {

    SYSTEM=`uname`

    if [ "${SYSTEM}" = "DragonFly" ] ; then
	OSLIBS="${OSLIBS} -pthread"
    fi

    ISBSD=`uname | grep -i bsd`
    if ! [ -z "${ISBSD}" ] ; then
	OSLIBS="${OSLIBS} -pthread"
    fi

    if [ -z "${PTHREAD_LIBS}" ] ; then
    	${CC} ${TH_TMPCPROGC} -o ${TH_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} 2>>/dev/null
    	ER=$?
    	if [ ${ER} -eq 0 ] ; then
    		return 1
    	else
    		${CC} ${TH_TMPCPROGC} -o ${TH_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} -pthread 2>>/dev/null
    		ER=$?
    		if [ ${ER} -eq 0 ] ; then
    			OSLIBS="${OSLIBS} -pthread"
    			return 1
    		else
    			${CC} ${TH_TMPCPROGC} -o ${TH_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} -lpthread 2>>/dev/null
    			ER=$?
    			if [ ${ER} -eq 0 ] ; then
    				OSLIBS="${OSLIBS} -lpthread"
    				return 1
				fi
    		fi
    	fi
    else
    	OSLIBS="${OSLIBS} ${PTHREAD_LIBS}"
    fi
		

    ${CC} ${TH_TMPCPROGC} -o ${TH_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} 2>>/dev/null
    ER=$?
    if [ ${ER} -eq 0 ] ; then
    	return 1
    else
    	${CC} ${TH_TMPCPROGC} -o ${TH_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} -D_GNU_SOURCE 2>>/dev/null
    	ER=$?
    	if [ ${ER} -eq 0 ] ; then
	    	${ECHO_CMD} "Older GNU pthread library found"
	    	OSCFLAGS="${OSCFLAGS} -D_GNU_SOURCE"
	    	return 1
	    else
    		${ECHO_CMD} "Do not use pthreads" 
		fi
    fi
    
    return 0
}

pthread_testbarriers() {

    ${ECHO_CMD} "pthread_barrier_t barrier;" >> ${TH_TMPCPROGC}
    ${CC} ${TH_TMPCPROGC} -o ${TH_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} 2>>/dev/null
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "pthread barriers not found"
		OSCFLAGS="${OSCFLAGS} -DTURN_NO_THREAD_BARRIERS"
    fi
}

dtls_testlib() {

    if [ -z "${TURN_NO_DTLS}" ] ; then
    	${CC} ${DTLS_TMPCPROGC} -o ${DTLS_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} 2>>/dev/null
    	ER=$?
    	if [ ${ER} -eq 0 ] ; then
    	    return 1
    	else
    	    return 0
    	fi
    else
		return 0
    fi
}

testdaemon() {

	${CC} ${D_TMPCPROGC} -o ${D_TMPCPROGB} ${OSCFLAGS} ${OSLIBS} 2>>/dev/null
	ER=$?
	if [ ${ER} -eq 0 ] ; then
	    OSCFLAGS="${OSCFLAGS} -DTURN_HAS_DAEMON"
	fi
}

test_sin_len() {
    TMPCADDRPROGC=src/client/ns_turn_ioaddr.c
    ${CC} -c ${OSCFLAGS} -DTURN_HAS_SIN_LEN -Isrc ${TMPCADDRPROGC} -o ${TMPCADDRPROGO} 2>>/dev/null
    ER=$?
    if [ ${ER} -eq 0 ] ; then
	OSCFLAGS="${OSCFLAGS} -DTURN_HAS_SIN_LEN"
	${ECHO_CMD} "Sockets code is fine: sin_len field present"
    else
	${CC} -c ${OSCFLAGS} -Isrc ${TMPCADDRPROGC} -o ${TMPCADDRPROGO} 2>>/dev/null
	ER=$?
	if [ ${ER} -eq 0 ] ; then
	    ${ECHO_CMD} "Sockets code is fine: no sin_len field present"
	else
	    ${ECHO_CMD} "WARNING: trial compilation failed: src/client/ns_turn_ioaddr.c"
	fi
    fi
}

#########################
# Start
#########################

cleanup

#########################
# To be set:
#########################

if [ -z "${ECHO_CMD}" ] ; then
	ECHO_CMD=echo 
fi

if [ -z "${PORTNAME}" ] ; then
	PORTNAME=turnserver
fi

#########################

# Installation directory options

ac_prev=
ac_dashdash=
for ac_option
do
  # If the previous option needs an argument, assign it.
  if test -n "$ac_prev"; then
    eval $ac_prev=\$ac_option
    ac_prev=
    continue
  fi

  case $ac_option in
  *=?*) ac_optarg=`expr "X$ac_option" : '[^=]*=\(.*\)'` ;;
  *=)   ac_optarg= ;;
  *)    ac_optarg=yes ;;
  esac

  # Accept the important Cygnus configure options, so we can diagnose typos.

  case $ac_dashdash$ac_option in
  --)
    ac_dashdash=yes ;;

  -bindir | --bindir | --bindi | --bind | --bin | --bi)
    ac_prev=BINDIR ;;
  -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*)
    BINDIR=$ac_optarg ;;

  -datadir | --datadir | --datadi | --datad | -schemadir | --schemadir)
    ac_prev=SCHEMADIR ;;
  -datadir=* | --datadir=* | --datadi=* | --datad=* | -schemadir=* | --schemadir=*)
    SCHEMADIR=$ac_optarg ;;

  -docdir | --docdir | --docdi | --doc | --do | -docsdir | --docsdir | --docsdi | --docs)
    ac_prev=DOCDIR ;;
  -docdir=* | --docdir=* | --docdi=* | --doc=* | --do=* | -docsdir=* | --docsdir=* | --docsdi=* | --docs=*)
    DOCSDIR=$ac_optarg ;;

  -examplesdir | --examplesdir | -examples | --examples)
    ac_prev=EXAMPLESDIR ;;
  -examplesdir=* | --examplesdir=* | -examples=* | --examples=*)
    EXAMPLESDIR=$ac_optarg ;;

  -includedir | --includedir | --includedi | --included | --include \
  | --includ | --inclu | --incl | --inc)
    ac_prev=INCLUDEDIR ;;
  -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \
  | --includ=* | --inclu=* | --incl=* | --inc=*)
    INCLUDEDIR=$ac_optarg ;;

  -turnincludedir | --turnincludedir)
    ac_prev=TURNINCLUDEDIR ;;
  -turnincludedir=* | --turnincludedir=*)
    TURNINCLUDEDIR=$ac_optarg ;;

  -libdir | --libdir | --libdi | --libd)
    ac_prev=LIBDIR ;;
  -libdir=* | --libdir=* | --libdi=* | --libd=*)
    LIBDIR=$ac_optarg ;;

  -mandir | --mandir | --mandi | --mand | --man | --ma | --m | -manprefix | --manprefix)
    ac_prev=MAXPREFIX ;;
  -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=* | -manprefix=* | --manprefix=*)
    MANPREFIX=$ac_optarg ;;

  -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
    ac_prev=PREFIX ;;
  -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
    PREFIX=$ac_optarg ;;

  -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \
  | --syscon | --sysco | --sysc | --sys | --sy | -confdir | --confdir)
    ac_prev=CONFDIR ;;
  -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \
  | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=* | -confdir=* | --confdir=*)
    CONFDIR=$ac_optarg ;;
    
  -disable-rpath | --disable-rpath)
  	TURN_DISABLE_RPATH=1 ;;
    
  esac
      
done

if test -n "$ac_prev"; then
  ac_option=--`echo $ac_prev | sed 's/_/-/g'`
  as_fn_error $? "missing argument to $ac_option"
fi

#############################################

if [ -z "${PREFIX}" ] ; then

	if [ -z "${prefix}" ] ; then

		SYSTEM=`uname`

		if [ "${SYSTEM}" = "NetBSD" ] ; then
			# A little tough guy
			PREFIX=/usr/pkg
		elif [ "${SYSTEM}" = "SunOS" ] ; then
			# A fat guy
			PREFIX=/usr
		else
			# An ordinary person
			PREFIX=/usr/local
		fi
	else
		PREFIX=${prefix}
	fi
fi

if [ -z "${BINDIR}" ] ; then
    if [ -z "${bindir}" ] ; then
	BINDIR=${PREFIX}/bin
    else
	BINDIR=${bindir}
    fi
fi

if [ -z "${CONFDIR}" ] ; then
    if [ -z "${confdir}" ] ; then
	CONFDIR=${PREFIX}/etc
    else
	CONFDIR=${confdir}
    fi
fi

if [ -z "${MANPREFIX}" ] ; then
    if [ -z "${manprefix}" ] ; then
	MANPREFIX=${PREFIX}
    else
	MANPREFIX=${manprefix}
    fi
fi

if [ -z "${EXAMPLESDIR}" ] ; then
    if [ -z "${examplesdir}" ] ; then
	EXAMPLESDIR=${PREFIX}/share/examples/${PORTNAME}
    else
	EXAMPLESDIR=${examplesdir}
    fi
fi

if [ -z "${DOCSDIR}" ] ; then
    if [ -z "${docsdir}" ] ; then
	DOCSDIR=${PREFIX}/share/doc/${PORTNAME}
    else
	DOCSDIR=${docsdir}
    fi
fi

if [ -z "${LIBDIR}" ] ; then
    if [ -z "${libdir}" ] ; then
	LIBDIR=${PREFIX}/lib
    else
	LIBDIR=${libdir}
    fi
fi

if [ -z "${SCHEMADIR}" ] ; then
    if [ -z "${schemadir}" ] ; then
	SCHEMADIR=${PREFIX}/share/${PORTNAME}
    else
	SCHEMADIR=${schemadir}
    fi
fi

if [ -z "${INCLUDEDIR}" ] ; then
    if [ -z "${includedir}" ] ; then
	INCLUDEDIR=${PREFIX}/include
    else
	INCLUDEDIR=${includedir}
    fi
fi

if [ -z "${TURNINCLUDEDIR}" ] ; then
    if [ -z "${turnincludedir}" ] ; then
	TURNINCLUDEDIR=${INCLUDEDIR}/turn
    else
	TURNINCLUDEDIR=${turnincludedir}
    fi
fi

###############################################

if [ -z "${ARCHIVERCMD}" ] ; then
	ARCHIVERCMD="ar -r"
fi

if [ -z "${MORECMD}" ]; then
	type more 2>>/dev/null
	ER=$?
	if [ ${ER} -eq 0 ] ; then
		MORECMD="more"
	else
		type less 2>>/dev/null
		ER=$?
		if [ ${ER} -eq 0 ] ; then
			MORECMD="less"
		else
			MORECMD="cat"
		fi
	fi
fi

OSCFLAGS="-I${INCLUDEDIR} -I${PREFIX}/include/ -I/usr/local/include ${CFLAGS}"
OSLIBS="${LDFLAGS}"
for ilib in ${PREFIX}/lib/event2/ ${PREFIX}/lib/ /usr/local/lib/event2/ /usr/local/lib/ ${PREFIX}/lib64/event2/ ${PREFIX}/lib64/ /usr/local/lib64/event2/ /usr/local/lib64/ 
do
    if [ -d ${ilib} ] ; then
		OSLIBS="${OSLIBS} -L${ilib}"
		TURN_RPATH="${TURN_RPATH} -Wl,-rpath,${ilib}"
    fi
done

SYSTEM=`uname`
if [ "${SYSTEM}" = "NetBSD" ] ; then
	OSCFLAGS="${OSCFLAGS} -I/usr/pkg/include"
	OSLIBS="-L/usr/pkg/lib ${OSLIBS}"
	if ! [ -z "${TURN_ACCEPT_RPATH}" ] ; then
	    TURN_RPATH="${TURN_RPATH} -Wl,-rpath,/usr/pkg/lib"
	fi
fi

###########################
# Install shell commands
###########################

type ginstall 2>>/dev/null
ER=$?
if [ ${ER} -eq 0 ] ; then
	INSTALL_PROGRAM="ginstall"
	INSTALL_MAN="ginstall"
	INSTALL_SCRIPT="ginstall"
	INSTALL_SHARED_LIB="ginstall"
	INSTALL_STATIC_LIB="ginstall"
	INSTALL_DATA="ginstall"
	MKDIR="ginstall -d"
else
	type install 2>>/dev/null
	ER=$?
	if [ ${ER} -eq 0 ] ; then
		INSTALL_PROGRAM="install"
		INSTALL_MAN="install"
		INSTALL_SCRIPT="install"
		INSTALL_SHARED_LIB="install"
		INSTALL_STATIC_LIB="install"
		INSTALL_DATA="install"
		MKDIR="install -d"
	else
		INSTALL_PROGRAM="cp -pf"
		INSTALL_MAN="cp -pf"
		INSTALL_SCRIPT="cp -pf"
		INSTALL_SHARED_LIB="cp -pf"
		INSTALL_STATIC_LIB="cp -pf"
		INSTALL_DATA="cp -pf"
		MKDIR="mkdir -p"
	fi
fi

type pkill 2>>/dev/null
ER=$?
if [ ${ER} -eq 0 ] ; then
	PKILL_PROGRAM="pkill"
else
	PKILL_PROGRAM="${ECHO_CMD}"
fi

INSTALL_DIR="cp -rpf"
MKBUILDDIR="mkdir -p"
RMCMD="rm -rf"

#############################
# Adjustments for Solaris
#############################

SYSTEM=`uname`

if [ "${SYSTEM}" = "SunOS" ] ; then
# Solaris ? is this you ?!
    OSCFLAGS="${OSCFLAGS} -D__EXTENSIONS__ -D_XOPEN_SOURCE=500"
    OSLIBS="${OSLIBS} -lnsl"
fi

#########################
# Temporary DIR location:
#########################

TMPDIR="."

if [ -d /var/tmp ] ; then
  TMPDIR="/var/tmp"
elif [ -d /tmp ] ; then
  TMPDIR=/tmp
fi

${ECHO_CMD} Use TMP dir ${TMPDIR}

#########################
# Basic C test programs
#########################

TMPCADDRPROGO=${TMPDIR}/__test__ccomp_addr_$$.o

TMPCPROG=__test__ccomp__$$
TMPCPROGC=${TMPDIR}/${TMPCPROG}.c
TMPCPROGB=${TMPDIR}/${TMPCPROG}

cat > ${TMPCPROGC} <<!
#include <stdlib.h>
int main(int argc, char** argv) {
    return (int)(argv[argc][0]);
}
!

TH_TMPCPROG=__test__ccomp__pthread__$$
TH_TMPCPROGC=${TMPDIR}/${TH_TMPCPROG}.c
TH_TMPCPROGB=${TMPDIR}/${TH_TMPCPROG}

cat > ${TH_TMPCPROGC} <<!
#include <stdlib.h>
#include <pthread.h>
int main(int argc, char** argv) {
    pthread_mutexattr_settype(0,PTHREAD_MUTEX_RECURSIVE);
    return (int)pthread_create(0,0,0,0)+(int)(argv[argc][0]);
}

!

DTLS_TMPCPROG=__test__ccomp__dtls__$$
DTLS_TMPCPROGC=${TMPDIR}/${DTLS_TMPCPROG}.c
DTLS_TMPCPROGB=${TMPDIR}/${DTLS_TMPCPROG}

cat > ${DTLS_TMPCPROGC} <<!
#include <stdlib.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
int main(int argc, char** argv) {
    return (((int)(BIO_CTRL_DGRAM_QUERY_MTU)) + argc + (int)(argv[argc][0]) + DTLSv1_listen(NULL,NULL));
}
!

D_TMPCPROG=__test__ccomp__daemon__$$
D_TMPCPROGC=${TMPDIR}/${D_TMPCPROG}.c
D_TMPCPROGB=${TMPDIR}/${D_TMPCPROG}

cat > ${D_TMPCPROGC} <<!
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv) {
    return (int)daemon(0,0)+(int)(argv[argc][0]);
}
!

E_TMPCPROG=__test__ccomp__libevent2__$$
E_TMPCPROGC=${TMPDIR}/${E_TMPCPROG}.c
E_TMPCPROGO=${TMPDIR}/${E_TMPCPROG}.o

cat > ${E_TMPCPROGC} <<!
#include <stdlib.h>
#include <event2/event.h>
int main(int argc, char** argv) {
    return (int)(argv[argc][0]);
}
!

HR_TMPCPROG=__test__ccomp__hiredis__$$
HR_TMPCPROGC=${TMPDIR}/${HR_TMPCPROG}.c
HR_TMPCPROGB=${TMPDIR}/${HR_TMPCPROG}

cat > ${HR_TMPCPROGC} <<!
#include <stdlib.h>
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
int main(int argc, char** argv) {
	redisAsyncHandleRead(NULL);
    return (int)(argv[argc][0]);
}
!

PQ_TMPCPROG=__test__ccomp__libpq__$$
PQ_TMPCPROGC=${TMPDIR}/${PQ_TMPCPROG}.c
PQ_TMPCPROGB=${TMPDIR}/${PQ_TMPCPROG}

cat > ${PQ_TMPCPROGC} <<!
#include <stdlib.h>
#include <libpq-fe.h>
int main(int argc, char** argv) {
    return (argc+(PQprotocolVersion(NULL))+(int)(argv[0][0]));
}
!

MYSQL_TMPCPROG=__test__ccomp__libmysql__$$
MYSQL_TMPCPROGC=${TMPDIR}/${MYSQL_TMPCPROG}.c
MYSQL_TMPCPROGB=${TMPDIR}/${MYSQL_TMPCPROG}

cat > ${MYSQL_TMPCPROGC} <<!
#include <stdlib.h>
#include <mysql.h>
int main(int argc, char** argv) {
    return (argc+
    	(int)(mysql_real_connect(NULL, NULL, NULL, NULL, NULL, 0, NULL, 0)!=0)+
    	(int)(argv[0][0]));
}
!

##########################
# What is our compiler ?
##########################

if [ -z "${CC}" ] ; then
    CC=cc
    ${CC} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
	ER=$?
	if ! [ ${ER} -eq 0 ] ; then
		CC=gcc
    	${CC} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
		ER=$?
		if ! [ ${ER} -eq 0 ] ; then
			CC=clang
    		${CC} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
			ER=$?
			if ! [ ${ER} -eq 0 ] ; then
				CC=unknown
			fi
		fi
	fi
fi

${ECHO_CMD} "Compiler: ${CC}"

if [ -z "${TURN_ACCEPT_RPATH}" ] ; then
    ${CC} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} -Wl,-rpath,/usr/lib 2>>/dev/null
    ER=$?
    if [ ${ER} -eq 0 ] ; then
	TURN_ACCEPT_RPATH=1
    fi
fi

${CC} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    ${ECHO_CMD} "ERROR: cannot use compiler ${CC} properly"
    cleanup
    exit
fi

###########################
# Check if we can use GNU
# or Clang compiler flags
###########################

GNUOSCFLAGS="-g ${GNUOSCFLAGS}"
GNUOSCFLAGS="${GNUOSCFLAGS} -Wall -Wno-deprecated-declarations -Wextra -Wformat-security -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith -Wcast-qual"
GNUOSCFLAGS="${GNUOSCFLAGS}"

${CC} -Werror ${GNUOSCFLAGS} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    ${ECHO_CMD} "Hm..."
    ${CC} -Wall ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "Not an ordinary GNU or Clang compiler"
    else
		${ECHO_CMD} "g++ or something..."
		GNUOSCFLAGS="-g -Wall -Wno-deprecated-declarations -Wextra -Wformat-security -Wpointer-arith -Wcast-qual"
		${CC} -Werror ${GNUOSCFLAGS} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
		ER=$?
		if ! [ ${ER} -eq 0 ] ; then
	    	${ECHO_CMD} "Not an ordinary g++ compiler"
	    	GNUOSCFLAGS="-x c++ -g -Wall -Wno-deprecated-declarations -Wextra -Wformat-security -Wpointer-arith -Wcast-qual"
	    	${CC} -Werror ${GNUOSCFLAGS} ${TMPCPROGC} ${OSCFLAGS} -o ${TMPCPROGB} 2>>/dev/null
	    	ER=$?
	    	if ! [ ${ER} -eq 0 ] ; then
	    		${ECHO_CMD} "Not an ordinary c++ compiler"
	    	else
				${ECHO_CMD} "Clang++ compiler ?"
	    		OSCFLAGS="${GNUOSCFLAGS} ${OSCFLAGS}"
	    	fi
		else
	    	OSCFLAGS="${GNUOSCFLAGS} ${OSCFLAGS}"
		fi
    fi
else
    OSCFLAGS="${GNUOSCFLAGS} ${OSCFLAGS}"
fi

###########################
# Test some general-purpose
# libraries 
###########################

testlib socket
testlib rt
testlib wldap32
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    echo "CYGWIN ?"
fi
testlib wldap64
testlib intl

###########################
# Test sockets compilation
###########################

test_sin_len

###########################
# Can we use multi-threading ?
###########################

pthread_testlib
ER=$?
if [ ${ER} -eq 0 ] ; then
	${ECHO_CMD} "ERROR: Cannot find pthread library functions."
	exit
fi

if [ -z ${TURN_NO_THREAD_BARRIERS} ] ; then 
	pthread_testbarriers
else 
	TURN_NO_THREAD_BARRIERS="-DTURN_NO_THREAD_BARRIERS"
fi

if [ -z ${TURN_IP_RECVERR} ] ; then 
	${ECHO_CMD} "Ignore IP_RECVERR"
else 
	${ECHO_CMD} "Use IP_RECVERR"
	TURN_IP_RECVERR="-DTURN_IP_RECVERR"
	OSCFLAGS="${OSCFLAGS} ${TURN_IP_RECVERR}"
fi

###########################
# Can we use daemon ?
###########################

testdaemon

###########################
# Test OpenSSL installation
###########################

testlib crypto
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    ${ECHO_CMD} "Crypto SSL lib found."
else
    ${ECHO_CMD} "ERROR: OpenSSL Crypto development libraries are not installed properly in required location."
    ${ECHO_CMD} "Abort."
    cleanup
    exit
fi

testlib ssl
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    ${ECHO_CMD} "SSL lib found."
else
    ${ECHO_CMD} "ERROR: OpenSSL development libraries are not installed properly in required location."
    ${ECHO_CMD} "Abort."
    cleanup
    exit
fi

###########################
# Can we use DTLS ?
###########################

if [ -z ${TURN_NO_DTLS} ] ; then 

dtls_testlib
ER=$?
if [ ${ER} -eq 0 ] ; then
	${ECHO_CMD} "WARNING: Cannot find DTLS support."
	${ECHO_CMD} "Turning DTLS off."
	TURN_NO_DTLS="-DTURN_NO_DTLS"
fi

else
	TURN_NO_DTLS="-DTURN_NO_DTLS"
fi

###########################
# Test Libevent2 setup
###########################
testlibevent2_comp
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    ${ECHO_CMD} "Libevent2 development found."
else
    ${ECHO_CMD} "ERROR: Libevent2 development libraries are not installed properly in required location."
    ${ECHO_CMD} "ERROR: may be you have just too old libevent tool - then you have to upgrade it."
    ${ECHO_CMD} "See the INSTALL file."
    ${ECHO_CMD} "Abort."
    cleanup
    exit
fi

testlib event_core
ER=$?
if ! [ ${ER} -eq 0 ] ; then
    ${ECHO_CMD} "Libevent2 runtime found."
else
    testlib event
    ER=$?
    if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "Libevent2 runtime found (old style)."
    else
		${ECHO_CMD} "ERROR: Libevent2 runtime libraries are not installed properly in required location."
		${ECHO_CMD} "See the INSTALL file."
		${ECHO_CMD} "Abort."
		cleanup
		exit
    fi
fi

if [ -z "${TURN_NO_TLS}" ] ; then

	testlib event_openssl
	ER=$?
	if ! [ ${ER} -eq 0 ] ; then
    	${ECHO_CMD} "Libevent2 openssl found."
	else
    	${ECHO_CMD} "ERROR: Libevent2 development libraries are not compiled with OpenSSL support."
    	${ECHO_CMD} "TLS will be disabled."
    	TURN_NO_TLS="-DTURN_NO_TLS"
	fi

else
	TURN_NO_TLS="-DTURN_NO_TLS"
fi

testlib event_pthreads
ER=$?
if ! [ ${ER} -eq 0 ] ; then
	${ECHO_CMD} "Libevent2 pthreads found."
else
   	${ECHO_CMD} "ERROR: Libevent2 development libraries are not compiled with threads support."
	exit
fi

###########################
# Test PostgreSQL
###########################

if [ -z "${TURN_NO_PQ}" ] ; then

	testlibpq
	ER=$?
	if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "PostgreSQL found."
	else
		TURN_NO_PQ="-DTURN_NO_PQ"
	fi
else
	TURN_NO_PQ="-DTURN_NO_PQ"
fi

###########################
# Test MySQL
###########################

if [ -z "${TURN_NO_MYSQL}" ] ; then

	testlibmysql
	ER=$?
	if ! [ ${ER} -eq 0 ] ; then
		${ECHO_CMD} "MySQL found."
	else
		TURN_NO_MYSQL="-DTURN_NO_MYSQL"
	fi
else
	TURN_NO_MYSQL="-DTURN_NO_MYSQL"
fi

###########################
# Test Redis
###########################

if [ -z "${TURN_NO_HIREDIS}" ] ; then

	testhiredis

	ER=$?
	if ! [ ${ER} -eq 0 ] ; then
  		${ECHO_CMD} "Hiredis found."
	else
  		TURN_NO_HIREDIS="-DTURN_NO_HIREDIS"
	fi

else
	TURN_NO_HIREDIS="-DTURN_NO_HIREDIS"

fi

###############################
# LDCONFIG
###############################

if [ -z "${LDCONFIG}" ] ; then
	ISBSD=`uname | grep -i bsd`
	if [ -z "${ISBSD}" ] ; then
		ISLINUX=`uname | grep -i linux`
		if [ -z "${ISLINUX}" ] ; then 
			SYSTEM=`uname`
			if [ "${SYSTEM}" = "SunOS" ] ; then
				LDCONFIG="crle -u -l"
			else
				LDCONFIG=${ECHO_CMD}
			fi
		else
			LDCONFIG="ldconfig -n"
		fi
	else
		LDCONFIG="ldconfig -m"
	fi
fi


###############################
# So, what we have now:
###############################

OSCFLAGS="${OSCFLAGS} ${TURN_NO_THREAD_BARRIERS} ${TURN_NO_DTLS} ${TURN_NO_TLS} -DINSTALL_PREFIX=${PREFIX}"

if ! [ -z "${TURN_ACCEPT_RPATH}" ] ; then
  if [ -z "${TURN_DISABLE_RPATH}" ] ; then
    TURN_RPATH="${TURN_RPATH} -Wl,-rpath,/usr/local/lib"
    OSLIBS="${OSLIBS} ${TURN_RPATH}"
  fi
fi

${ECHO_CMD} PREFIX="${PREFIX}" OSLIBS="${OSLIBS}" DBLIBS="${DBLIBS}" OSCFLAGS="${OSCFLAGS}" DBCFLAGS="${DBCFLAGS}" $@

###############################
# Make make:
###############################

${ECHO_CMD} "#################################" > Makefile
${ECHO_CMD} "# Generated by configure script #" >> Makefile
${ECHO_CMD} "#################################" >> Makefile
${ECHO_CMD} "ECHO_CMD = ${ECHO_CMD}" >> Makefile
${ECHO_CMD} "CC = ${CC}" >> Makefile
${ECHO_CMD} "LDFLAGS += ${OSLIBS}" >> Makefile
${ECHO_CMD} "DBLIBS += ${DBLIBS}" >> Makefile
${ECHO_CMD} "CFLAGS += ${OSCFLAGS}" >> Makefile
${ECHO_CMD} "CPPFLAGS = ${CPPFLAGS}" >> Makefile
${ECHO_CMD} "DBCFLAGS += ${DBCFLAGS} ${TURN_NO_PQ} ${TURN_NO_MYSQL} ${TURN_NO_HIREDIS}" >> Makefile
${ECHO_CMD} "#" >> Makefile
${ECHO_CMD} "PORTNAME = ${PORTNAME}" >> Makefile
${ECHO_CMD} "PREFIX = ${PREFIX}" >> Makefile
${ECHO_CMD} "prefix = ${PREFIX}" >> Makefile
${ECHO_CMD} "BINDIR = ${BINDIR}" >> Makefile
${ECHO_CMD} "bindir = ${BINDIR}" >> Makefile
${ECHO_CMD} "CONFDIR = ${CONFDIR}" >> Makefile
${ECHO_CMD} "confdir = ${CONFDIR}" >> Makefile
${ECHO_CMD} "MANPREFIX = ${MANPREFIX}" >> Makefile
${ECHO_CMD} "manprefix = ${MANPREFIX}" >> Makefile
${ECHO_CMD} "EXAMPLESDIR = ${EXAMPLESDIR}" >> Makefile
${ECHO_CMD} "examplesdir = ${EXAMPLESDIR}" >> Makefile
${ECHO_CMD} "DOCSDIR = ${DOCSDIR}" >> Makefile
${ECHO_CMD} "docsdir = ${DOCSDIR}" >> Makefile
${ECHO_CMD} "LIBDIR = ${LIBDIR}" >> Makefile
${ECHO_CMD} "libdir = ${LIBDIR}" >> Makefile
${ECHO_CMD} "SCHEMADIR = ${SCHEMADIR}" >> Makefile
${ECHO_CMD} "schemadir = ${SCHEMADIR}" >> Makefile
${ECHO_CMD} "INCLUDEDIR = ${INCLUDEDIR}" >> Makefile
${ECHO_CMD} "includedir = ${INCLUDEDIR}" >> Makefile
${ECHO_CMD} "TURNINCLUDEDIR = ${TURNINCLUDEDIR}" >> Makefile
${ECHO_CMD} "turnincludedir = ${TURNINCLUDEDIR}" >> Makefile
${ECHO_CMD} "#" >> Makefile
${ECHO_CMD} "ARCHIVERCMD = ${ARCHIVERCMD}" >> Makefile
${ECHO_CMD} "MKDIR = ${MKDIR}" >> Makefile
${ECHO_CMD} "INSTALL_PROGRAM = ${INSTALL_PROGRAM}" >> Makefile
${ECHO_CMD} "PKILL_PROGRAM = ${PKILL_PROGRAM}" >> Makefile
${ECHO_CMD} "INSTALL_MAN = ${INSTALL_MAN}" >> Makefile
${ECHO_CMD} "INSTALL_SCRIPT = ${INSTALL_SCRIPT}" >> Makefile
${ECHO_CMD} "INSTALL_SHARED_LIB = ${INSTALL_SHARED_LIB}" >> Makefile
${ECHO_CMD} "INSTALL_STATIC_LIB = ${INSTALL_STATIC_LIB}" >> Makefile
${ECHO_CMD} "INSTALL_DATA = ${INSTALL_DATA}" >> Makefile
${ECHO_CMD} "INSTALL_DIR = ${INSTALL_DIR}" >> Makefile
${ECHO_CMD} "MKBUILDDIR = ${MKBUILDDIR}" >> Makefile
${ECHO_CMD} "RMCMD = ${RMCMD}" >> Makefile
${ECHO_CMD} "MORECMD = ${MORECMD}" >> Makefile
${ECHO_CMD} "LDCONFIG=${LDCONFIG}" >> Makefile
${ECHO_CMD} "################################" >> Makefile
${ECHO_CMD} "" >> Makefile
cat Makefile.in >> Makefile

###############################
# End:
###############################

cleanup


